package org.cttic.visualization.visualization.z_service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import org.apache.http.HttpHost;
import org.bson.Document;
import org.cttic.visualization.visualization.common.EsDataIncomplete;
import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.cttic.visualization.visualization.util.*;
import org.cttic.visualization.visualization.util.itineraryQuery.queryUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.tripUtil;
import org.cttic.visualization.visualization.util.trackQuery.trackQuery;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.cttic.visualization.visualization.z_service.z_TrackQuery;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2020-05-08 16:08
 * 2.3.2车辆轨迹点查询接口
 */
@Service
public class z_TrackQueryImpl implements z_TrackQuery {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private trackQuery trackQuery;
    @Autowired
    private queryUtil queryTime;
    @Override
    public String z_trackQuery(String jsonData) throws ParseException {
        JSONObject Data = ParseJsonDataUtil.getJsonData(jsonData);
        String provinceId = Data.getString("provinceId");
        //车牌_颜色
        String vehiclesId = Data.getString("vehiclesId");
        String vehPlate=null;
        String vehPlateColor=null;
        if (vehiclesId.contains("_")){
            String[] split = vehiclesId.split("_");
             vehPlate=split[0];
             vehPlateColor =split[1];
        }
        int Color = ConvertColor.toColor(Integer.parseInt(vehPlateColor));
        String queryType = Data.getString("queryType");
        String queryValue1 = Data.getString("queryValue1");
        trackQueryData trackQueryData = new trackQueryData();
        List<trackQueryData.vehLocation> list = new ArrayList<>();

        HttpHost[] httpHostArray = new HttpHost[1];
        httpHostArray[0] = new HttpHost("192.168.195.32",9200, "http");
//        httpHostArray[1] = new HttpHost("192.168.195.32",9200, "http");
//        httpHostArray[2] = new HttpHost("192.168.195.32",9200, "http");

        // 创建RestHighLevelClient客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(httpHostArray));
        //todo 按行程编码查询
        if(queryType.equals("2")){
            trackQuery.trackQuery(provinceId,vehPlate,Color+"",queryValue1,list);
                trackQueryData.setVehLocationList(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String json = mapper.writeValueAsString(trackQueryData);
                    return json;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

        //todo 按时间段查询
        }else if (queryType.equals("1")){
            String queryValue2 = Data.getString("queryValue2");     //结束时间
            String startTime = DateUtil.TtoTime(queryValue1);
            String endTime = DateUtil.TtoTime(queryValue2);
            System.out.println("****************车辆轨迹点时间段查询开始*****************");
            trackQuery.esQueryTravel(esClient,provinceId,vehPlate,Color,startTime,endTime,list);

//            String queryValue2 = Data.getString("queryValue2");     //结束时间
//            String startTime = DateUtil.TtoTime(queryValue1);
//            String endTime = DateUtil.TtoTime(queryValue2);
//            System.out.println("****************车辆轨迹点时间段查询开始*****************");
//            trackQuery.trackQueryByTime(provinceId,vehPlate,Color+"",startTime,endTime,list);
//
            trackQueryData.setVehLocationList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(trackQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println("****************车辆轨迹点时间段查询结束*****************");

            //todo 按时间点查询
        }else if (queryType.equals("0")){
            System.out.println("****************车辆轨迹点时间点查询开始*****************");
            String Time = DateUtil.TtoTime(queryValue1);
            String stamp = DateUtil.dateToStamp(Time);
            String startTime = DateUtil.StampToDate(Long.parseLong(stamp)-3600000);
            String endTime = DateUtil.StampToDate(Long.parseLong(stamp)+3600000);
            trackQuery.esQueryTravel(esClient,provinceId,vehPlate,Color,startTime,endTime,list);
            trackQueryData.setVehLocationList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(trackQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println("****************车辆轨迹点时间点查询结束*****************");
        }
        return null;
    }
}
